package com.ht.web.student;

import com.ht.bean.json.JsonData;
import com.ht.bean.student.Major;
import com.ht.bean.student.Studentclass;
import com.ht.bean.student.Studentfloor;
import com.ht.bean.student.Studenthuor;
import com.ht.service.emp.EmpinfoService;
import com.ht.service.student.StudentService;
import com.ht.service.student.StudentfallService;
import com.ht.service.student.StudentfloorService;
import com.ht.service.student.StudenthuorService;
import com.ht.util.Pager;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author xmf
 * @date 2020/6/9 10:18
 */
@Controller
@RequestMapping("studentHuor")
public class studenthuorController {
    @Resource
    private JsonData jsonData;
    @Resource
    private StudenthuorService studenthuorService;//宿舍管理接口
    @Resource
    private StudentfloorService studentfloorService;//楼栋管理接口
    @Resource
    private StudentService studentService;//学生管理接口
    //表格数据渲染所要的分页数据
    @RequestMapping("data")
    @ResponseBody
    public JsonData data(@Param("limit")int limit , @Param("page")int page,@Param("studenthuor")Studenthuor studenthuor){
        Pager pager = new Pager();
        pager.setPageSize(limit);
        pager.setCurrPage(page);
        jsonData.setCount(studenthuorService.selCount(studenthuor));
        jsonData.setData(studenthuorService.sybase(pager,studenthuor));
        return jsonData;
    }

    //去宿舍页面
    @RequestMapping("list")
    public String list(Map map){
        List<Studentfloor> selstufloor = studentfloorService.selList();
        map.put("list",selstufloor);
        return "student/studentHuor";
    }



    //去添加宿舍页面
    @RequestMapping("/toAdd")
    public String toAdd(Map map) {
        List<Studentfloor> studentFloorList = studentfloorService.selList();
        map.put("studentFloorList",studentFloorList);
        return "student/studentHuorAdd";
    }


    //去修改宿舍页面
    @RequestMapping("/toUpdate")
    public String toUpdate(Map map,int id) {
        List<Studentfloor> studentFloorList = studentfloorService.selList();
        map.put("studentFloorList",studentFloorList);
        Studenthuor studenthuor = studenthuorService.selectByPrimaryKey(id);
        map.put("studenthuor",studenthuor);
        Studentfloor studentfloor = studentfloorService.selectByPrimaryKey(studenthuor.getFloorid());
        map.put("studentfloor",studentfloor);
        return "student/studentHuorUpdate";
    }

    //添加宿舍
    @RequestMapping("/add")
    @ResponseBody
    public Integer add(Studenthuor studenthuor) {
        List<Studenthuor> studenthuorList=studenthuorService.selByFid(studenthuor.getFloorid());
        for (Studenthuor studenthuor1:studenthuorList) {
            if (studenthuor.getHuorname().equals(studenthuor1.getHuorname()) || studenthuor1.getHuorname()==studenthuor.getHuorname()
            ){
                return 0;
            }
        }
        studenthuor.setCount(0);
        Integer i = studenthuorService.insert(studenthuor);
        return i;
    }

    //修改宿舍
    @RequestMapping("/update")
    @ResponseBody
    public Integer upd(Studenthuor studenthuor){
        Studenthuor studenthuor1 = studenthuorService.selectByPrimaryKey(studenthuor.getHourid());

        if(studenthuor.getCount() > studenthuor.getNumberbeds()){
            return 0;
        }
        Integer j = studenthuorService.selByFidFname(studenthuor.getFloorid(),studenthuor.getHuorname());
        if(j==0 ||  studenthuor1.getHuorname().trim().equals(studenthuor.getHuorname().trim())){
            int   i1= studenthuorService.updateByPrimaryKeySelective(studenthuor);
            return i1;
        }
        return 0;

    }


    //删除宿舍
    @RequestMapping("/del")
    @ResponseBody
    public String del(Integer id) {
        int c= studentService.selbyhuorid(id);
        if(c==0){
            studenthuorService.deleteByPrimaryKey(id);
            return "true";
        }else {
            return "false";
        }
    }

}
